//
//  LGMyOrderViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/14.
//

#import "LGServiceOrderViewController.h"
#import "LGServiceOrderDetailController.h"
#import "LGServiceOrderSearchController.h"
#import "LGServiceOrderTableViewCell.h"
#import "LGScreenOrderView.h"
@interface LGServiceOrderViewController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *searchBtn;
/**   */
@property (nonatomic , strong)UIButton *screenBtn;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *statusArray;
/**   */
@property (nonatomic , copy)NSString *type;
/**   */
@property (nonatomic , copy)NSString *status;
/**   */
@property (nonatomic , copy)NSString *isRefuned;

@end

@implementation LGServiceOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.type = @"";
    self.isRefuned = @"";
    self.title = @"服务包订单";
    [self.view addSubview:self.searchBtn];
    [self.view addSubview:self.screenBtn];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
    [self requestOptions];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/vip/doctorviplists" parameters:@{@"is_refuned":self.isRefuned,@"keyword":@"",@"vip_id":self.type} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无订单" offset:0];
        }
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
}

//请求筛选状态
-(void)requestOptions{
    [RequestUtil withPOST:@"/doctor/im/vipList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            NSMutableArray *array = [NSMutableArray array];
            [array insertObject:@{@"value":@"",@"title":@"全部"} atIndex:0];
            NSArray *vipArray = returnData;
            for (NSInteger i=0; i<vipArray.count; i++) {
                NSDictionary *tempDic = vipArray[i];
                [array addObject:@{@"value":LGNSString(tempDic[@"vip_id"]),@"title":tempDic[@"title"]}];
            }
            self.statusArray = array;
        }
        
    } error:^(NSError *error) {
        
    }];
}

-(void)searchBtnAction{
    LGServiceOrderSearchController *controller = [[LGServiceOrderSearchController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)screenBtnAction{
    if(self.statusArray.count>0){
        LGScreenOrderView *screenView = [[LGScreenOrderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        screenView.typeArray = self.statusArray;
        screenView.type = self.type;
        screenView.status = @"";
        screenView.statusArray = @[@{@"value":@"",@"title":@"全部"},@{@"value":@"N",@"title":@"已支付"},@{@"value":@"Y",@"title":@"已退款"}];
        [self.view.window addSubview:screenView];
        __weak typeof(self) weakSelf = self;
        screenView.screenOrderResult = ^(NSString * _Nonnull type, NSString * _Nonnull status) {
            weakSelf.type = type;
            weakSelf.isRefuned = status;
            [weakSelf requestData];
        };
        
    }else{
        [self requestOptions];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGServiceOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGServiceOrderDetailController *controller = [[LGServiceOrderDetailController alloc]init];
    controller.isRefuned = LGNSString(tempDic[@"is_refund"]);
    controller.appointId = LGNSString(tempDic[@"vip_id"]);
    [self.navigationController pushViewController:controller animated:YES];
}


-(UIButton *)searchBtn{
    if(!_searchBtn){
        _searchBtn = [UIButton buttonWithTitle:@"输入患者姓名或订单编号查询" titleFont:LGFont(14) textColor:@"#999999" imageName:@"searchTheme22"];
        _searchBtn.frame = CGRectMake(viewPix(16), viewPix(15), Screen_W-viewPix(105), viewPix(34));
        _searchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _searchBtn.layer.borderColor = [UIColor colorWithString:@"#F0F0F0"].CGColor;
        _searchBtn.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _searchBtn.layer.borderWidth = 1;
        _searchBtn.cornerRidus = viewPix(17);
        _searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        _searchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(15), 0, 0);
        [_searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

-(UIButton *)screenBtn{
    if(!_screenBtn){
        _screenBtn = [UIButton buttonWithTitle:@"筛选" titleFont:LGFont(15) textColor:@"#666666" imageName:@"我的订单-筛选"];
        _screenBtn.frame = CGRectMake(Screen_W-viewPix(85), viewPix(16), viewPix(83), viewPix(32));
        [_screenBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
        [_screenBtn addTarget:self action:@selector(screenBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _screenBtn;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(56), Screen_W, Screen_H-topBarHeight-viewPix(56)) style:UITableViewStylePlain
        ];
        [_tableView registerClass:[LGServiceOrderTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = viewPix(148);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
